//
//  STCommentTableViewCell.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/24.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import UIKit
import Kingfisher

class STCommentTableViewCell: UITableViewCell {
    
    private let avatarImageView = UIImageView()
    private let userNameLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = #colorLiteral(red: 0.01680417731, green: 0.1983509958, blue: 1, alpha: 0.8013314261)
        return label
    }()
    private let contentLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.black
        label.numberOfLines = 6
        return label
    }()
    
    private let timeLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Helvetica-Light", size: 10)
        label.textColor = UIColor.gray
        return label
    }()
    
    private let replayBgView : UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.init(white: 0, alpha: 0.05)
        view.layer.cornerRadius = 8
        return view
    }()
    
    private let replayLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "PingFangSC-Ultralight", size: 10)
        label.textColor = UIColor.gray
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        avatarImageView.layer.cornerRadius = 16
        avatarImageView.layer.masksToBounds = true
        self.contentView.addSubview(avatarImageView)
        self.contentView.addSubview(userNameLabel)
        self.contentView.addSubview(contentLabel)
        self.contentView.addSubview(timeLabel)
        self.contentView.addSubview(replayBgView)
        self.contentView.addSubview(replayLabel)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func configureCell(model : STCommentInfo) {
        self.avatarImageView.kf.setImage(with: URL(string: model.userInfo.avatarUrl ?? ""), placeholder:UIImage(color: UIColor.gray))
        self.userNameLabel.text = model.userInfo.username
        self.contentLabel.text = model.commentInfo.content
        self.timeLabel.text = model.commentInfo.createdAt.natureFormat
        self.replayLabel.text = String(model.replayCount) + "回复"
        self.setNeedsUpdateConstraints()
    }
    
    override func updateConstraints() {
        super.updateConstraints()
        self.avatarImageView.snp.updateConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(8)
            make.size.equalTo(CGSize(width: 32, height: 32))
        }
        self.userNameLabel.snp.updateConstraints { (make) in
            make.top.equalTo(self.avatarImageView).offset(3)
            make.left.equalTo(self.avatarImageView.snp_right).offset(5)
            make.right.equalToSuperview().offset(-18)
        }
        self.contentLabel.snp.updateConstraints { (make) in
            make.top.equalTo(self.userNameLabel.snp_bottom).offset(10)
            make.left.equalTo(self.userNameLabel)
            make.right.equalToSuperview().offset(-18)
        }
        self.timeLabel.snp.updateConstraints { (make) in
            make.top.equalTo(self.contentLabel.snp_bottom).offset(10)
            make.left.equalTo(self.userNameLabel)
            make.bottom.equalToSuperview().offset(-5)
        }
        self.replayBgView.snp.updateConstraints { (make) in
            make.left.equalTo(self.timeLabel.snp_right).offset(4)
            make.width.equalTo(self.replayLabel).offset(8)
            make.height.equalTo(16)
            make.centerY.equalTo(self.timeLabel)
        }
        self.replayLabel.snp.updateConstraints { (make) in
            make.center.equalTo(self.replayBgView)
        }
    }
}
